package com.itbaizhan.openhis.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.constants.Constants;
import com.itbaizhan.openhis.domain.Role;
import com.itbaizhan.openhis.dto.RoleDto;
import com.itbaizhan.openhis.service.RoleService;
import com.itbaizhan.openhis.mapper.RoleMapper;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

/**
 * @author MR
 * @description 针对表【sys_role(角色信息表)】的数据库操作Service实现
 * @createDate 2025-06-26 22:42:07
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
		implements RoleService {
	/**
	 * 添加角色
	 * @param roleDto
	 * @return
	 */
	@Override
	public int addRole(RoleDto roleDto) {
		Role role = new Role();
		BeanUtils.copyProperties(roleDto, role);
		role.setCreateBy(roleDto.getSimpleUser().getUserName());
		role.setCreateTime(DateUtil.date());
		return roleMapper.insert(role);
		
	}
	/**
	 * 修改角色
	 * @param roleDto
	 * @return
	 */
	@Override
	public int updateRole(RoleDto roleDto) {
		Role role = new Role();
		BeanUtils.copyProperties(roleDto, role);
		role.setUpdateBy(roleDto.getSimpleUser().getUserName());
		role.setUpdateTime(DateUtil.date());
		return roleMapper.updateById(role);
	}
	
	@Autowired
	private RoleMapper roleMapper;
	
	/**
	 * 分页条件查询角色
	 * @param roleDto
	 * @return
	 */
	@Override
	public DataGridView listRoleForPage(RoleDto roleDto) {
		Page<Role> page = new Page<>(roleDto.getPageNum(), roleDto.getPageSize());
		QueryWrapper<Role> wrapper = new QueryWrapper<>();
		wrapper.like(StringUtils.isNotBlank(roleDto.getRoleName()),
				Role.COL_ROLE_NAME, roleDto.getRoleName());
		wrapper.like(StringUtils.isNotBlank(roleDto.getRoleCode()),
				Role.COL_ROLE_CODE, roleDto.getRoleCode());
		wrapper.eq(StringUtils.isNotBlank(roleDto.getStatus()),
				Role.COL_STATUS,roleDto.getStatus());
		wrapper.ge(roleDto.getBeginTime() != null,
				Role.COL_CREATE_TIME, roleDto.getBeginTime());
		wrapper.le(roleDto.getEndTime() != null,
				Role.COL_CREATE_TIME, roleDto.getEndTime());
		wrapper.orderByAsc(Role.COL_ROLE_SORT);
		roleMapper.selectPage(page, wrapper);
		return new DataGridView(page.getTotal(),page.getRecords());
	}
	
	/**
	 * 给用户分配角色
	 *
	 * @param userId
	 * @param roleIds
	 */
	@Override
	public int saveRoleUser(Long userId, Long[] roleIds) {
		roleMapper.deleteRoleUserByUserIds(Arrays.asList(userId));
		for (Long roleId : roleIds) {
			roleMapper.saveRoleUser(userId, roleId);
		}
		return 0;
	}
	
	/**
	 * 查询所有角色
	 * @return
	 */
	@Override
	public List<Role> listAllRoles() {
		QueryWrapper<Role> wrapper = new QueryWrapper<>();
		wrapper.eq(Role.COL_STATUS, Constants.STATUS_TRUE);
		wrapper.orderByAsc(Role.COL_ROLE_SORT);
		return roleMapper.selectList( wrapper);
	}
	
	/**
	 * 根据用户id查询角色id
	 * @param userId
	 * @return
	 */
	@Override
	public List<Long> getRoleIdsByUserId(Long userId) {
		return roleMapper.getRoleIdsByUserId(userId);
	}
	
	/**
	 * 根据角色id查询角色
	 * @param roleId
	 * @return
	 */
	@Override
	public Role getOne(Long roleId) {
		return roleMapper.selectById(roleId);
	}
	
	/**
	 * 批量删除角色
	 * @param roleIds
	 * @return
	 */
	@Override
	public int deleteRoleByIds(Long[] roleIds) {
		List<Long> ids = Arrays.asList(roleIds);
		if (ids != null && ids.size() > 0){
			//1.删除角色菜单sys_role_menu表关联数据
			roleMapper.deleteRoleMenuByRoleIds(ids);
			//2.删除用户角色关系sys_role_user表关联数据
			roleMapper.deleteRoleUserByRoleIds(ids);
			//3.删除角色表数据
			return roleMapper.deleteBatchIds(ids);
		}else {
			return 0;
		}
	}
	
	/**
	 * 给角色分配菜单
	 * @param roleId
	 * @param menuIds
	 */
	@Override
	public void saveRoleMenu(Long roleId, Long[] menuIds) {
		//1.删除角色菜单关系sys_role_menu表关联数据
		roleMapper.deleteRoleMenuByRoleIds(Arrays.asList(roleId));
		for (Long menuId : menuIds){
			roleMapper.saveRoleMenu(roleId,menuId);
		}
	}
}




